package com.fjec.devops.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.common.enums.EnumSysCode;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.DateUtil;
import com.fjec.common.view.AnnexBizItem;
import com.fjec.devops.entity.BizYsOfflineReg;
import com.fjec.devops.entity.BizYsOfflineRegResult;
import com.fjec.devops.feign.BaseClient;
import com.fjec.devops.mapper.BizYsOfflineRegResultMapper;
import com.fjec.devops.service.IBizYsOfflineRegResultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.devops.view.YsOfflineRegResultVO;
import com.fjec.devops.view.YsOfflineRegVO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 024.银盛线下进件结果表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-11-23
 */
@Service
public class BizYsOfflineRegResultServiceImpl extends ServiceImpl<BizYsOfflineRegResultMapper, BizYsOfflineRegResult> implements IBizYsOfflineRegResultService {

    @Resource
    private BizYsOfflineRegResultMapper mapper;

    @Resource
    private BaseClient baseClient;

    @Override
    public JsonResult saveYsOfflineRegResult(Long fileId, String fileName) {
        BizYsOfflineRegResult entity = new BizYsOfflineRegResult();
        entity.setFileId(fileId);
        entity.setFileName(fileName);
        entity.setHandleStatus(0);
        entity.setSuccessNum(0);
        entity.setFaultNum(0);
        entity.setTimes(0);
        entity.setCreateTime(DateUtil.nowLdt());
        mapper.insert(entity);
        // 使附件生效
        List<AnnexBizItem> itemList = new ArrayList<>();
        itemList.add(new AnnexBizItem(fileId, entity.getId(), 113));
        baseClient.enableAnnexStatus(itemList);
        return JsonResult.success()
                .code(EnumSysCode.MSG_6002.getCode())
                .message(EnumSysCode.MSG_6002.getMsg())
                .build();
    }

    @Override
    public JsonResult getEntityListPage(Page page) throws Exception {
        QueryWrapper<BizYsOfflineRegResult> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("CreateTime");
        Page<BizYsOfflineRegResult> returnPage = mapper.selectPage(page, queryWrapper);
        List<YsOfflineRegResultVO> resultList = new ArrayList<>();
        for (BizYsOfflineRegResult entity : returnPage.getRecords()) {
            YsOfflineRegResultVO view = new YsOfflineRegResultVO();
            BeanUtils.copyProperties(view, entity);
            resultList.add(view);
        }
        return JsonResult.success()
                .total(returnPage.getTotal())
                .data(resultList)
                .build();
    }

    @Override
    public List<BizYsOfflineRegResult> getUnhandledEntityList(int limit) {
        return mapper.getUnhandledEntityList(limit);
    }
}
